Skip to content

Conversation

@melver
Copy link
Contributor

@melver melver commented Nov 26, 2025

Add a helper function getAllocTokenModeAsString() to convert AllocTokenMode values to their string representation.

NFC.

Add a helper function getAllocTokenModeAsString() to convert
AllocTokenMode values to their string representation.

NFC.
@llvmbot
Copy link
Member

llvmbot commented Nov 26, 2025

@llvm/pr-subscribers-llvm-support

Author: Marco Elver (melver)

Changes

Add a helper function getAllocTokenModeAsString() to convert AllocTokenMode values to their string representation.

NFC.


Full diff: https://github.com/llvm/llvm-project/pull/169650.diff

3 Files Affected:

  • (modified) clang/lib/Frontend/CompilerInvocation.cpp (+1-15)
  • (modified) llvm/include/llvm/Support/AllocToken.h (+3)
  • (modified) llvm/lib/Support/AllocToken.cpp (+14)
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index c7c29a91721c0..81824a76ea3d0 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3956,21 +3956,7 @@ void CompilerInvocationBase::GenerateLangArgs(const LangOptions &Opts,
                 std::to_string(*Opts.AllocTokenMax));
 
   if (Opts.AllocTokenMode) {
-    StringRef S;
-    switch (*Opts.AllocTokenMode) {
-    case llvm::AllocTokenMode::Increment:
-      S = "increment";
-      break;
-    case llvm::AllocTokenMode::Random:
-      S = "random";
-      break;
-    case llvm::AllocTokenMode::TypeHash:
-      S = "typehash";
-      break;
-    case llvm::AllocTokenMode::TypeHashPointerSplit:
-      S = "typehashpointersplit";
-      break;
-    }
+    StringRef S = llvm::getAllocTokenModeAsString(*Opts.AllocTokenMode);
     GenerateArg(Consumer, OPT_falloc_token_mode_EQ, S);
   }
 }
diff --git a/llvm/include/llvm/Support/AllocToken.h b/llvm/include/llvm/Support/AllocToken.h
index e40d8163a9d7c..1dc3a0cacef24 100644
--- a/llvm/include/llvm/Support/AllocToken.h
+++ b/llvm/include/llvm/Support/AllocToken.h
@@ -46,6 +46,9 @@ inline constexpr AllocTokenMode DefaultAllocTokenMode =
 LLVM_ABI std::optional<AllocTokenMode>
 getAllocTokenModeFromString(StringRef Name);
 
+/// Returns the canonical string name for the given AllocTokenMode.
+LLVM_ABI StringRef getAllocTokenModeAsString(AllocTokenMode Mode);
+
 /// Metadata about an allocation used to generate a token ID.
 struct AllocTokenMetadata {
   SmallString<64> TypeName;
diff --git a/llvm/lib/Support/AllocToken.cpp b/llvm/lib/Support/AllocToken.cpp
index daa40d4e9dcc6..cabe52189c4bb 100644
--- a/llvm/lib/Support/AllocToken.cpp
+++ b/llvm/lib/Support/AllocToken.cpp
@@ -28,6 +28,20 @@ llvm::getAllocTokenModeFromString(StringRef Name) {
       .Default(std::nullopt);
 }
 
+StringRef llvm::getAllocTokenModeAsString(AllocTokenMode Mode) {
+  switch (Mode) {
+  case AllocTokenMode::Increment:
+    return "increment";
+  case AllocTokenMode::Random:
+    return "random";
+  case AllocTokenMode::TypeHash:
+    return "typehash";
+  case AllocTokenMode::TypeHashPointerSplit:
+    return "typehashpointersplit";
+  }
+  llvm_unreachable("Unknown AllocTokenMode");
+}
+
 static uint64_t getStableHash(const AllocTokenMetadata &Metadata,
                               uint64_t MaxTokens) {
   return getStableSipHash(Metadata.TypeName) % MaxTokens;

@melver melver merged commit 637f206 into llvm:main Nov 26, 2025
9 of 11 checks passed
tanji-dg pushed a commit to tanji-dg/llvm-project that referenced this pull request Nov 27, 2025
Add a helper function getAllocTokenModeAsString() to convert
AllocTokenMode values to their string representation.

NFC.
GeneraluseAI pushed a commit to GeneraluseAI/llvm-project that referenced this pull request Nov 27, 2025
Add a helper function getAllocTokenModeAsString() to convert
AllocTokenMode values to their string representation.

NFC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants